'''
Created on Jun 20, 2012

@author: alebalbin
'''
import logging
import argparse
import sys
import os
import datetime
import subprocess

from lib.base import strandDNATree
from lib.parse_gtf2 import read_gtf

def build_it(gtf_file):
    
    transc_types=['lincRNA','protein_coding','misc_RNA',
                 'polymorphic_pseudogene','misc_RNA_pseudogene',
                 'pseudogene', 'miRNA', 'processed_transcript',
                 'miRNA_pseudogene']
    
    gtf_file =open(gtf_file)
    mydna=strandDNATree()    
    filter_transc=True
    i=0
    for transcript in read_gtf(gtf_file):
        if filter_transc and (transc_types is not None):
            if (transcript.feature in transc_types):
                i+=1
                mydna.grow_strand( transcript )
                
        elif not filter_transc:
            mydna.grow_strand( transcript )        
    gtf_file.close()
    
    # Do the intersection
    min_start_coord=0
    positive_strand=mydna.genome['+']    
     
    sense_trans= positive_strand.find(min_start_coord,mydna.maxPosStrand)
    #print len(sense_trans)
    neg_strand='-'
    for t in sense_trans:
        nat_tot = mydna.sense_nat_pair(t,neg_strand)
        if nat_tot is not None:
            print ",".join(map(str,nat_tot)).replace(',','\t')
    print i

def main():
    logging.basicConfig(level=logging.DEBUG,
                        format="%(asctime)s - %(name)s - %(levelname)s - %(message)s")
    logging.info("==========================")
    parser = argparse.ArgumentParser()
    parser.add_argument("--g", dest="gtf_file", default=None)
    args = parser.parse_args()
    build_it(args.gtf_file)

if __name__ == '__main__': 
    sys.exit(main())

'''        
def read_gene_list(ifile):
    ifile=open(ifile)
    actionable_genes=deque()
    for l in ifile:
        fields = l.strip('\n').split('\t')
        start, end, value=int(fields[1]),int(fields[2]), {'chr':fields[0], 'annot':fields[3],'strand':fields[5]}
        actionable_genes.append( Gene(start, end, value) )
    
    return actionable_genes
    
       def __init__(self, int start, int end, object value=None, object chrom=None, object strand=None ):
        assert start <= end, "start must be less than end"


'''